﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using simpleHotel.Client.UserControls;
using simpleHotel.Client.Common;

namespace simpleHotel.Client.Admin
{
    public partial class ucLogin : BaseUserControl
    {
        
        HotelReference.HotelService objService = new HotelReference.HotelService();

        public ucLogin()
        {
            InitializeComponent();
            
        }

        private bool LogIn(string user, string pass)
        {
            HotelReference.ObjAdminDTO obj = new HotelReference.ObjAdminDTO();
            DataTable dt = new DataTable();
            obj.UserName = textEdit1.Text.Trim();
            obj.Password = textEdit2.Text.Trim();

            dt = objService.IsLogin(obj);

            if (dt.Rows.Count > 0)
            {
                Common.Common.UserName = dt.Rows[0]["Username"].ToString();
                Common.Common.HotelID = int.Parse(dt.Rows[0]["HotelID"].ToString());
                return true;
            }
            else
            {
                //-2: login fail, -1: admin, 1-...:hotel
                Common.Common.HotelID = -2;
                return false;
            }
        }

        private void ucLogin_Load(object sender, EventArgs e)
        {
            this.FormMain.EnableFeaturesFirstTime();
            this.textEdit1.Focus();
        }

        private void btnLogin_Click_1(object sender, EventArgs e)
        {
            if (!LogIn(textEdit1.Text.Trim(), textEdit2.Text.Trim()))
            {
                //do some thing
                MessageBox.Show("Sai Username/Password.", "Thông báo");
            }
            else
            {
                //do some thing               
                var role = objService.GetRoleByID(Common.Common.HotelID);
                this.FormMain.EnableFeatures(role.ToLower() == Common.Common.AdminRole);
                this.FormMain.OpenHotelList();
                this.FormMain.CloseTab("start");
            }
        }

        private void textEdit2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.btnLogin_Click_1(this.pictureBox1, EventArgs.Empty);
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.btnLogin_Click_1(sender, e);
        }
    }
}
